<?php
if (!defined('ROOT')) {
   die("Impossible de voir ce fichier directement.");
}
include_once ROOT."/inc/classes/Form.php";

class Model
{
    var $table;
    var $index;
    var $fields;
    var $validators;
       
    //TIMESTAMPS
    var $updated;
    var $created;
    
    function __construct()
    {
        $this->getFields();
    
    }
    function getFields()
    {
        //Completed in extended Model
    }
    function setFields()
    {}
    
    function getForm($inputs,$method = 'post',$action='',$id='')
    {
     
      $form = new Form($this,$inputs,$method,$action,$id);
      return $form->getForm();
    }
  
   
    
  
   function add() {
      global $DB;
        
      //Timestamps
      $this->created = CurrentDateTime();
      $this->updated = CurrentDateTime();     
        
        $this->getFields();
      
      $nb_fields = count($this->fields);
      if ($nb_fields>0) {
         // Build query
         $query = "INSERT
                   INTO `".$this->table."` (";

         $i = 0;
         foreach ($this->fields as $key => $val) {
            $fields[$i] = $key;
            $values[$i] = $val;
            $i++;
         }

         for ($i=0 ; $i<$nb_fields; $i++) {
            $query .= "`".$fields[$i]."`";
            if ($i!=$nb_fields-1) {
               $query .= ",";
            }
         }

         $query .= ") VALUES (";
         for ($i=0 ; $i<$nb_fields ; $i++) {

            if ($values[$i]=='NULL') {
               $query .= $values[$i];
            } else {
               $query .= "'".$values[$i]."'";
            }

            if ($i!=$nb_fields-1) {
               $query .= ",";
            }

         }
         $query .= ")";

         if ($result=mysql_query($query,$DB->mysql_handler)) {
            $this->fields[$this->index] = mysql_insert_id();
            $this->setFields();
            return $this->fields[$this->index];
         }

      }
      return false;
   }

    function findById($ID,$return = false) {
      global $DB;
      
      if (strlen($ID)==0) {
         return false;
      }

      $query = "SELECT *
                FROM `".$this->table."`
                WHERE `".$this->index."` = '$ID'";

      if ($result = mysql_query($query,$DB->mysql_handler)) {
         if (mysql_num_rows($result)==1) {
            $data = mysql_fetch_assoc($result);
            if($return)
               return $data;
            else
            {
               $this->fields = $data;
               $this->setFields();
               return true;
            }
         }
      }

      return false;
   }


     function update() {
      global $DB, $TIMESTAMP;
      
      //Update timestamp
      if (isset($this->updated)) $this->updated = CurrentDateTime();
      
      $this->getFields();
    
     $updates = array_keys($this->findById($this->fields[$this->index],true));
       
      foreach ($updates as $field) {
         if (isset($this->fields[$field])) {
            $query  = "UPDATE `".$this->table."`
                       SET `".$field."`";

            if ($this->fields[$field]=="NULL") {
               $query .= " = ".$this->fields[$field];

            } else {
               $query .= " = '".$this->fields[$field]."'";
            }

            $query .= " WHERE `id` ='".$this->fields[$this->index]."'";

            if (!mysql_query($query,$DB->mysql_handler)) {
               return false;
            }
         } 
      }
      $this->setFields();
      return true;
   }
   
    function find ($condition="", $order="", $limit="") {
      global $DB;
     
      $query = "SELECT *
                FROM `".$this->table."`";

      if (!empty($condition)) {
         $query .= " WHERE $condition";
      }

      if (!empty($order)) {
         $query .= " ORDER BY $order";
      }

      if (!empty($limit)) {
         $query .= " LIMIT ".intval($limit);
      }

      $data = array();
      if ($result = mysql_query($query,$DB->mysql_handler)) {
         if (mysql_num_rows($result)) {
            while ($line = mysql_fetch_assoc($result)) {
               $data[$line['id']] = $line;
            }
         }
      }

      return $data;
   }





      
   

}

